﻿using Avalonia;
using Avalonia.ReactiveUI;
using System;
using System.IO;
using Serilog;
using torch.plugin_manager;
using VehicleManager.Utils;

namespace torch.ui;

sealed class Program
{
    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args)
    {
        LogHelper.initLog();
        CreatePluginFolder();
        // LoadPlugin();

        BuildAvaloniaApp()
            .StartWithClassicDesktopLifetime(args);
    }

    // Avalonia configuration, don't remove; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace()
            .UseReactiveUI();

    private static void LoadPlugin()
    {
        string currentDirectory = Directory.GetCurrentDirectory();
        string pluginDirectory = Path.Combine(currentDirectory, "plugin");
        PluginManager pluginManager = new PluginManager(pluginDirectory);
        var list = pluginManager.LoadPlugins();
        foreach (var plugin in list)
        {
            Log.Information($"已加载插件: {plugin.Name}");
        }
    }
    
    private static void CreatePluginFolder()
    {
        try
        {
            string currentDirectory = Directory.GetCurrentDirectory();
            string pluginDirectory = Path.Combine(currentDirectory, "plugin");
            if (!Directory.Exists(pluginDirectory))
            {
                Directory.CreateDirectory(pluginDirectory);
                Log.Information($"已成功创建plugin目录: {pluginDirectory}");
            }
            else
            {
                Log.Information($"目录已存在: {pluginDirectory}");
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex, "创建目录时发生错误: " + ex.Message);
        }
    }
}